package com.qiaoda.web;

import com.qiaoda.model.User;
import com.qiaoda.service.UserRepository;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by yangkuan on 2017/3/7.
 * 注意地址端口号是9300
 */
@RestController
@RequestMapping("/es")
public class EsController {

    @Autowired
    private UserRepository userRepository;

    @RequestMapping("/test")
    public void save(){
        userRepository.save(new User(3,"lisi","hello lisi"));
    }

    @RequestMapping("/findbyid")
    public User findById(){
        User one = userRepository.findOne(3);
        System.out.println(one.toString());
        return one;
    }

    @RequestMapping("/findByUsername")
    public User findByUsername(){
        User lisi = userRepository.findByUsername("lisi");
        System.out.println(lisi.toString());
        return lisi;
    }

    @RequestMapping("/findByTitle")
    public void findByTitle(){
        MultiMatchQueryBuilder query = QueryBuilders.multiMatchQuery("hello","title");
        Iterable<User> search = userRepository.search(query);
        System.out.println(search.toString());
    }

}
